Numerics Example Using PowerPC Assembly Language
Listing 11-1 is a code example that shows when the PowerPC assembly-language numeric features might be useful. The instructions used in this example are described in the Motorola PowerPC 601 RISC Microprocessor User's Manual. This example evaluates the polynomial3+2x2+-5
It illustrates the evaluation of a polynomial
0xn+c1xn+-1+...+cn
using Horner's recurrence
0
j for j = 1ton
On entry, general-purpose register GPR0 contains the degree n (<256) of the polynomial, and floating-point register F1 points to a function argument x. The coefficient table consists of double-format coefficients, starting with 0 . In this particular polynomial, 0 = 1, c1= 2, c2 = 0, and c3 .
Listing 11-1 Polynomial evaluation
r0: equ 0 # general-purpose register 0 r5: equ 5 # general-purpose register 5 f0: equ 0 # floating-point register 0 f1: equ 1 # floating-point register 1 f2: equ 2 # floating-point register 2 CTR: equ 9 # Count Register for loops extern polyeval{DS} # export the routine descriptor extern .polyeval # export the entry point # put the code in a program control section csect polyeval{PR} #high-level languages prepend a period to function names .polyeval: lwz r0,0(r5) # r0 = degree lfd f0,4(r5) # f0 = leading coefficient, c0 addic r5,r5,4 # r5 = address of leading coeff. &c0 mtspr CTR,r0 # CTR = r0 loop: lfdu f2,8(r5) # f2 = next coefficient # update r5 = r5 + 8 fmadd f0,f0,f1,f2 # f0 = f0 * f1 + f2; ... # res = res * x + c[j] bdnz loop # CTR = CTR - 1, branch if CTR0 fmr f1,f0 # f1 = f0 blr # return through the Link Register nop # # Set up the table of contents. It must include at least the # exported routines. It may also contain global data or pointers # to data. # polyeval_TOC: tc polyeval{tc}, polyeval{PR} # # Build a transition vector for all exported routines so they can # be accessed through an inter-TOC call. # csect polyeval{DS} # it's in a separate control section dc.l .polyeval # contains the entry point dc.l 0 # loader will fill in correct TOC # pointer dc.l 0 # save space for environment pointer